package io.github.shang11925.sbvuewelearn.controller;

import io.github.shang11925.sbvuewelearn.dao.ChaptersRepository;
import io.github.shang11925.sbvuewelearn.dto.ChaptersDTO;
import io.github.shang11925.sbvuewelearn.entity.Chapters;
import io.github.shang11925.sbvuewelearn.response.ChaptersResponse;
import io.github.shang11925.sbvuewelearn.service.ChaptersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("http://localhost:5173/")
public class ChaptersController {
    @Autowired
    ChaptersService chaptersService;
    @Autowired
    ChaptersRepository chaptersRepository;
    @GetMapping("/list-chapters")
    public ChaptersResponse findAll() {
        List<Chapters> chapters = chaptersService.findAll();
        ChaptersResponse chaptersResponse = new ChaptersResponse();
        chaptersResponse.setChapters(chapters);
        return chaptersResponse;
    }

    @GetMapping("/list-chapters/{courseId}")
    public ChaptersResponse list(@PathVariable Long courseId) {
        List<Chapters> chapters = chaptersService.findByCourseId(courseId);
        ChaptersResponse chaptersResponse = new ChaptersResponse();
        chaptersResponse.setChapters(chapters);
        return chaptersResponse;
    }

    @PostMapping("/add-chapters")
    public void add(@RequestBody ChaptersDTO chaptersDTO) {
        chaptersService.add(chaptersDTO);
    }
    @DeleteMapping("/delete-chapters/{id}")
    public void delete(@PathVariable Long id){
        chaptersService.delete(id);
    }
    @PutMapping("/update-chapters/{id}")
    public void update(@PathVariable Long id, @RequestBody ChaptersDTO chaptersDTO) {
        chaptersService.update(id, chaptersDTO);
    }
    @GetMapping("count")
    public long count(){
        return chaptersService.count();
    }
}
